package io.terminus.spzx.manager.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.manager.service.CategoryService;
import io.terminus.spzx.model.entity.product.Category;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 分类管理Controller
 * @Date 2024-09-21
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "分类管理接口")
@RequestMapping(value = "/admin/product/category")
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 分类列表，每次查询一层数据: SELECT * FROM category WHERE parent_id=#{id}
     *
     * @param id 分类ID
     * @return 分类列表
     */
    @GetMapping("/list/{id}")
    public Result<List<Category>> listCategory(@PathVariable Long id) {
        List<Category> list = categoryService.listCategory(id);

        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 分类列表
     *
     * @param id 分类ID
     * @return 分类列表
     */
    @GetMapping("/listParent/{id}")
    public Result<List<Category>> listParentCategory(@PathVariable Long id) {
        List<Category> list = categoryService.listParentCategory(id);

        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 导出类目的Excel数据
     *
     * @param response 响应数据
     */
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        categoryService.exportData(response);
    }

    /**
     * 导入类目的Excel数据
     *
     * @param file 导入文件
     * @return 导入结果
     */
    @PostMapping("/importData")
    public Result<Void> importData(MultipartFile file) {
        categoryService.importData(file);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
